package com.rfid.manager;

import com.rfid.a252robot.App;
import com.rfid.db.bean.Book;
import com.rfid.db.bean.BookDao;
import com.rfid.db.bean.DaoSession;
import com.rfid.db.bean.FailBook;
import com.rfid.db.bean.FailBookDao;
import com.rfid.db.bean.OperateInfo;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;

public class DBManager {

    public static List<Book> getBooks(int boxNum){
        DaoSession daoSession = App.getApp().getDaoSession();
        List<Book> operateAssets = daoSession.getBookDao().queryBuilder().where(BookDao.Properties.BoxNum.eq(boxNum)).build().list();
        return operateAssets;
    }

    public static Book getBook(String barcode){
        DaoSession daoSession = App.getApp().getDaoSession();
        List<Book> books = daoSession.getBookDao().queryBuilder().where(BookDao.Properties.Barcode.eq(barcode)).build().list();
        if (books.size() > 0) {
            return books.get(0);
        }
        return null;
    }

    public static void deleteBook(String barcode) {
        DaoSession daoSession = App.getApp().getDaoSession();
        Book book = getBook(barcode);
        if (book != null) {
            daoSession.getBookDao().delete(book);
            addOperateLog(book,OperateInfo.OPERATE_BORROW);
        }
    }

    public static void deleteBook(Book book) {
        DaoSession daoSession = App.getApp().getDaoSession();
        daoSession.getBookDao().delete(book);
    }

    public static void insertBook(Book book) {
        insertBook(book,OperateInfo.OPERATE_RESTORE);
    }

    public static void insertBook(Book book,int type) {
        DaoSession daoSession = App.getApp().getDaoSession();
        daoSession.getBookDao().insert(book);
        addOperateLog(book,type);
    }

    public static void updateBook(Book book) {
        DaoSession daoSession = App.getApp().getDaoSession();
        daoSession.getBookDao().update(book);
        addOperateLog(book,OperateInfo.OPERATE_UPDATE_INFO);
    }

    public static void addOperateLog(Book book,int type) {
        OperateInfo info = new OperateInfo();
        info.setBarcode(book.getBarcode());
        info.setBookName(book.getName());
        info.setBoxNum(book.getBoxNum());
        info.setUser(book.getPreBorrowReader());
        info.setOperate(type);
        DaoSession daoSession = App.getApp().getDaoSession();
        daoSession.getOperateInfoDao().insert(info);
    }

    public static List<Book> getBooksByRange(int start,int end) {
        DaoSession daoSession = App.getApp().getDaoSession();
        WhereCondition condition = daoSession.getBookDao().queryBuilder().and(BookDao.Properties.BoxNum.ge(start),BookDao.Properties.BoxNum.lt(end));
        List<Book> operateAssets = daoSession.getBookDao().queryBuilder().where(condition).build().list();
        return operateAssets;
    }

    public static List<Book> getBooks() {
        DaoSession daoSession = App.getApp().getDaoSession();
        List<Book> operateAssets = daoSession.getBookDao().queryBuilder().build().list();
        return operateAssets;
    }

    public static List<Book> getCanBorrowBooks() {
        DaoSession daoSession = App.getApp().getDaoSession();
        List<Book> operateAssets = daoSession.getBookDao().queryBuilder().where(BookDao.Properties.CanBorrow.eq(true)).build().list();
        return operateAssets;
    }

    public static void insertFailBook(FailBook fb) {
        DaoSession daoSession = App.getApp().getDaoSession();
        daoSession.getFailBookDao().insert(fb);
    }

    public static List<FailBook> getFailBooks() {
        DaoSession daoSession = App.getApp().getDaoSession();
        return daoSession.getFailBookDao().queryBuilder().build().list();
    }

    public static FailBook getFailBook(String barcode) {
        DaoSession daoSession = App.getApp().getDaoSession();
        List<FailBook> fbs = daoSession.getFailBookDao().queryBuilder().where(FailBookDao.Properties.Barcode.eq(barcode)).build().list();
        if (fbs.size() > 0) {
            return fbs.get(0);
        }
        return null;
    }

    public static void deleteFailBook(FailBook book) {
        DaoSession daoSession = App.getApp().getDaoSession();
        daoSession.getFailBookDao().delete(book);
    }
}
